/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package maincz.tests.webapp.service;

import maincz.tests.webapp.dao.IUserDAO;
import maincz.tests.webapp.entity.User;
import org.hibernate.Hibernate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 *
 * @author Main
 */
@Service
public class UserService implements IUserService {

    @Autowired
    private IUserDAO userDao;
        
    @Override
    @Transactional
    public User getUser(String username) {
        User user = userDao.getUser(username);
        if (user != null) {
            Hibernate.initialize(user);
            Hibernate.initialize(user.getRoles());
        }
        else {
            throw new UsernameNotFoundException("User " + username + " not found.");
        }
        return user;
    }
    
    @Override
    @Transactional
    public User getUser(String username, String pwdHash) {
        User user = userDao.getUser(username, pwdHash);
        if (user != null) {
            Hibernate.initialize(user);
            Hibernate.initialize(user.getRoles());
        }
        else {
            throw new UsernameNotFoundException("User " + username + " not found.");
        }
        return user;
    }
    
}
